package com.guowen.myaccount;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;

import androidx.appcompat.app.AppCompatActivity;

import com.guowen.myaccount.adapter.AccountAdapter;
import com.guowen.myaccount.db.AccountBean;
import com.guowen.myaccount.db.DBManager;

import java.util.List;


public class RecommendActivity extends AppCompatActivity implements View.OnClickListener{
    ListView recommendLv;//展示推荐的ListView
    AccountAdapter adapter;
    List<AccountBean> mDatas;
    Context context = this;
    Recommender recommender = new Recommender();
    Button btnHaochi, btnPianyi;
    ImageView backIv;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.recommend);
        recommendLv = findViewById(R.id.recommend_lv);
        btnHaochi = findViewById(R.id.recommend_haochi);
        btnPianyi = findViewById(R.id.recommend_pianyi);

        backIv = findViewById(R.id.activity_recommend_back);

        btnHaochi.setOnClickListener(this);
        btnPianyi.setOnClickListener(this);
        backIv.setOnClickListener(this);

        btnHaochi.setOnClickListener(this);
        btnPianyi.setOnClickListener(this);
        // 设置适配器
        mDatas = DBManager.getAllCanYanAccountFromRecommendtb();
        adapter = new AccountAdapter(this,mDatas);
        recommendLv.setAdapter(adapter);

        setLVClickListener();
    }
    //设置ListView每一个item的长按事件
    private void setLVClickListener() {
        recommendLv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                AccountBean accountBean = mDatas.get(position);
                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                builder.setTitle("您确定要删除这个记录吗？").setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        DBManager.deleteItemFromRecommendtbById(accountBean.getId());
                        mDatas.remove(position);
                        adapter.notifyDataSetChanged();
                    }
                }).setNegativeButton("取消",null).show();
                //在推荐表内删除该推荐条目
                return false;
            }
        });
    }
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.activity_recommend_back:
                finish();
                break;
            case R.id.recommend_haochi:
                //点击好吃后，将选择结果反馈给推荐算法，再弹出进一步选择对话框
                //选完后，执行相应推荐，并推送到ListView上
                if(recommender.getDelicacy() == 0 && recommender.getPrice() == 0){
                    recommender.setMoreDelicacy();
                }
                else{
                    recommender.addDelicacy();
                }
                mDatas = recommender.getRecommendList();
                adapter = new AccountAdapter(this,mDatas);
                adapter.notifyDataSetChanged();
                recommendLv.setAdapter(adapter);
                break;
            case R.id.recommend_pianyi:
                //点击便宜后，将选择结果反馈给推荐算法，弹出进一步选择对话框
                //选完后，执行相应推荐，并推送到ListView上
                if(recommender.getDelicacy() == 0 && recommender.getPrice() == 0){
                    recommender.setLessPrice();
                }
                else{
                    recommender.addPrice();
                }
                mDatas = recommender.getRecommendList();
                adapter = new AccountAdapter(this,mDatas);
                adapter.notifyDataSetChanged();
                recommendLv.setAdapter(adapter);
                break;
        }
    }

}